package com.haulmont.asu.core.scripts

import com.haulmont.asu.entity.ThemeBookingCommand
import com.haulmont.cuba.core.EntityManager
import com.haulmont.cuba.core.Persistence
import com.haulmont.cuba.core.Transaction
import com.haulmont.cuba.core.global.AppBeans
import org.apache.commons.lang.exception.ExceptionUtils
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

/**
 * Author: Sergey42
 * Date: 24.11.13 18:18
 */

Log log = LogFactory.getLog(ProjectThemeBookingScript.class);

ThemeBookingCommand command = params.get('Item');

Persistence persistence = AppBeans.get(Persistence.class);
Transaction tx = persistence.createTransaction();
Date projectDate = null;
String cathedraName = null;
Date certDate = null;
try {
    EntityManager em = persistence.getEntityManager();
    ThemeBookingCommand parent = (ThemeBookingCommand) command.getParentCommand();
    parent = em.find(ThemeBookingCommand.class, parent.id);
    command = em.find(ThemeBookingCommand.class, command.id);

    projectDate = parent.getCreateDate();
    cathedraName = command.getCathedra().getName();

    certDate = command.getCertificationDate();
} catch (Exception ex) {
    log.error(ExceptionUtils.getStackTrace(ex));
} finally {
    tx.end();
}

return [['cathedra': cathedraName, 'projectDate': projectDate, 'date': certDate]]